package com.sunny.fetch.common;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author: sublun
 * @Date: 2021/3/25 18:44
 */
public class RequestParam extends HashMap{
    private RequestParam(String apiName, Map params, List<String> fields) {
        put("api_name", apiName);
        put("token", SysConfig.get().getAccessToken());
        put("params", params);
        //String fieldsStr = fields.stream().collect(Collectors.joining(","));
        String fieldsStr = String.join(",", fields);
        put("fields", fieldsStr);
    }

    public static class RequestParamBuilder{
        private List<String> fields;
        private Map params;
        private String apiName;
        private RequestParamBuilder(String apiName) {
            this.apiName = apiName;
            params = new HashMap();
            fields = new ArrayList<String>();
        }

        public RequestParamBuilder setParams(Map params) {
            this.params.putAll(params);
            return this;
        }
        public RequestParamBuilder setParam(String key, String val) {
            params.put(key, val);
            return this;
        }
        public RequestParamBuilder setFields(List fields) {
            this.fields.addAll(fields);
            return this;
        }
        public RequestParamBuilder setField(String field) {
            this.fields.add(field);
            return this;
        }
        public RequestParam build() {
            return new RequestParam(apiName, params, fields);
        }
    }

    public static RequestParamBuilder builder(String apiName) {
        return new RequestParamBuilder(apiName);
    }



}
